~ chicken-core (master) /manual/Module (chicken port)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken port)56This module contains various extended port definitions.78All errors related to failing port operations will signal a condition9of kind {{exn}}.1011* New in CHICKEN 5.4.0: Errors caused by underlying C calls that12 change errno will produce a condition object with an {{errno}}13 property, which can be accessed with14 {{(get-condition-property <the-condition-object> 'exn 'errno)}}.1516=== Port attributes1718==== port-encoding1920<procedure>(port-encoding PORT)</procedure>2122Returns the encoding used for reading and writing data from and to23the given port. Encoding is currently one of the symbols {{utf-8}},24{{latin-1}} or {{binary}}. Note that the encoding can be changed by using25the (SRFI-17) setter procedure for this operation:2627{{(set! (port-encoding PORT) ENCODING)}}2829==== port-name3031<procedure>(port-name [PORT])</procedure>3233Fetch filename from {{PORT}}. This returns the filename that was used to open34this file. Returns a special tag string, enclosed into parentheses for35non-file ports. {{PORT}} defaults to the value of {{(current-input-port)}}.3637Note that the encoding can be changed by using38the (SRFI-17) setter procedure for this operation:3940{{(set! (port-name PORT) STRING)}}414243==== port-position4445<procedure>(port-position [PORT])</procedure>4647Returns the current position of {{PORT}} as two values: row and column number.48If the port does not support such an operation an error is signaled. This49procedure is currently only available for input ports. {{PORT}} defaults to the50value of {{(current-input-port)}}.515253=== Setting the file buffering mode5455==== set-buffering-mode!5657<procedure>(set-buffering-mode! PORT MODE [BUFSIZE])</procedure>5859Sets the buffering-mode for the file associated with {{PORT}} to60{{MODE}}, which should be one of the keywords {{#:full}},61{{#:line}} or {{#:none}}. If {{BUFSIZE}} is specified it62determines the size of the buffer to be used (if any).6364This procedure doesn't work on custom ports, such as those created with {{make-input-port}} or {{make-output-port}}.6566=== Terminal ports6768==== terminal-name6970<procedure>(terminal-name PORT)</procedure>7172Returns the name of the terminal that is connected to {{PORT}}.7374On Windows, this procedure always raises an exception.7576==== terminal-port?7778<procedure>(terminal-port? PORT)</procedure>7980Returns {{#t}} if {{PORT}} is connected to a terminal and81{{#f}} otherwise.828384==== terminal-size8586<procedure>(terminal-size PORT)</procedure>8788Returns two values, the number of rows and columns of the terminal89that is connected to {{PORT}} or {{0}}, {{0}} if the terminal size can90not be obtained.9192On Windows, this procedure always raises an exception.939495=== Input/output port extensions9697==== with-output-to-port9899<procedure>(with-output-to-port PORT THUNK)</procedure>100101Call procedure {{THUNK}} with the current output-port temporarily102bound to {{PORT}}.103104==== make-input-port105106<procedure>(make-input-port READ-CHAR CHAR-READY? CLOSE #!key peek-char read-bytevector read-line)</procedure>107108Returns a custom input port. Common operations on this port are109handled by the given parameters, which should be procedures of no110arguments. The following arguments are all different kinds of reader111procedures:112113* {{READ-CHAR}} is the most fundamental reader, and must always be114present. It is a thunk which is called when the next character is115to be read and it should return a character or {{#!eof}}.116* {{CHAR-READY?}} is a thunk which is called when {{char-ready?}}117is called on this port and should return {{#t}} or {{#f}}.118* {{CLOSE}} is a thunk which is called when the port is closed.119* {{peek-char}} is a thunk which is called when {{peek-char}} is120called on this port and should return a character or {{#!eof}}. If it121is not provided or {{#f}}, {{READ-CHAR}} will be used instead and the122created port object handles peeking automatically (by calling {{READ}}123and buffering the character).124* {{read-bytevector}} is called when {{read-bytevector}} or {{read-string!}} is called (or the125higher-level non-mutating {{read-string}} and {{read-bytevector}}). It will be invoked with 4126arguments: the port created by {{make-input-port}}, the number of127bytes to read, a bytevector to read into (which may be128assumed to be big enough to hold the data) and the offset into the129buffer at which to put the data to read. It should return the number130of bytes that have successfully been read, which should always be131equal to the requested bytes unless EOF was hit, in which case it can132be less. If this procedure is not provided or {{#f}}, the buffer will133be filled by repeated reads to {{READ-CHAR}}.134* {{READ-LINE}} is called when {{read-line}} is called. It will be135invoked with two arguments: the port created by {{make-input-port}}136and the maximum number of characters to read (or {{#f}}). If this137procedure is not provided or {{#f}}, the buffer will be filled by138repeated reads to {{READ-CHAR}}.139140All the optional procedures except for {{PEEK-CHAR}} are responsible141for updating the port's position, which currently can only be done via142low-level slot accessors like {{##sys#setslot}}; slot 4 is the row143number (ie, the line) and slot 5 is the column number (ie, the144character on the line). If the port's positions are not updated,145{{port-position}} won't work.146147Note that reading binary input from a custom input is only possible148when the {{read-bytevector}} operation is given, as byte-input can149currently not ben synthesized from character-input operations.150151152==== make-output-port153154<procedure>(make-output-port WRITE CLOSE #!key force-output)</procedure>155156Returns a custom output port. Common operations on this port are handled157by the given parameters, which should be procedures. {{WRITE}} is158called when output is sent to the port and receives a single argument,159a string. {{CLOSE}} is called when the port is closed and should160be a procedure of no arguments. {{force-output}} (if provided) is called161for flushing the output port.162163164==== with-error-output-to-port165166<procedure>(with-error-output-to-port PORT THUNK)</procedure>167168Call procedure {{THUNK}} with the current error output-port169temporarily bound to {{PORT}}.170171172==== with-input-from-port173174<procedure>(with-input-from-port PORT THUNK)</procedure>175176Call procedure {{THUNK}} with the current input-port temporarily177bound to {{PORT}}.178179180=== String-port extensions181182==== call-with-input-string183184<procedure>(call-with-input-string STRING PROC)</procedure>185186Calls the procedure {{PROC}} with a single argument that is a187string-input-port with the contents of {{STRING}}.188189190==== call-with-output-string191192<procedure>(call-with-output-string PROC)</procedure>193194Calls the procedure {{PROC}} with a single argument that is a195string-output-port. Returns the accumulated output-string.196197198==== with-input-from-string199200<procedure>(with-input-from-string STRING THUNK)</procedure>201202Call procedure {{THUNK}} with the current input-port temporarily203bound to an input-string-port with the contents of {{STRING}}.204205206==== with-output-to-string207208<procedure>(with-output-to-string THUNK)</procedure>209210Call procedure {{THUNK}} with the current output-port temporarily211bound to a string-output-port and return the accumulated output string.212213==== with-error-output-to-string214215<procedure>(with-error-output-to-string THUNK)</procedure>216217Call procedure {{THUNK}} with the current error output-port218temporarily bound to a string-output-port and return the accumulated219output string.220221222=== Port iterators223224==== port-for-each225226<procedure>(port-for-each FN THUNK)</procedure>227228Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, discarding the results.229230==== port-map231232<procedure>(port-map FN THUNK)</procedure>233234Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, returning a list of the collected results.235236==== port-fold237238<procedure>(port-fold FN ACC THUNK)</procedure>239240Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}}, (typically {{read}}) passing the {{ACC}} value as the second argument. The {{FN}} result becomes the new {{ACC}} value. When {{THUNK}} returns {{#!eof}}, the last {{FN}} result is returned.241242==== copy-port243244<procedure>(copy-port FROM TO [READ [WRITE]])</procedure>245246Reads all remaining data from port {{FROM}} using the reader procedure247{{READ}} and writes it to port {{TO}} using the writer procedure248{{WRITE}}. {{READ}} defaults to {{read-char}} and {{WRITE}} to249{{write-char}}. Note that this procedure does not check {{FROM}} and250{{TO}} for being ports, so the reader and writer procedures may251perform arbitrary operations as long as they can be invoked252as {{(READ FROM)}} and {{(WRITE X TO)}}, respectively.253{{copy-port}} returns an undefined value.254255{{copy-port}} was introduced in CHICKEN 4.6.0.256257=== Funky ports258259==== make-bidirectional-port260261<procedure>(make-bidirectional-port INPUT-PORT OUTPUT-PORT)</procedure>262263Returns a joint input/output port that proxies port operations to the264given {{INPUT-PORT}} and {{OUTPUT-PORT}}, respectively. This port265satisfies both {{input-port?}} and {{output-port?}}, and its two266directions may be closed independently.267268==== make-broadcast-port269270<procedure>(make-broadcast-port PORT ...)</procedure>271272Returns a custom output port that emits everything written into it to273the ports given as {{PORT ...}}. Closing the broadcast port does not close274any of the argument ports.275276==== make-concatenated-port277278<procedure>(make-concatenated-port PORT1 PORT2 ...)</procedure>279280Returns a custom input port that reads its input from {{PORT1}}, until it281is empty, then from {{PORT2}} and so on. Closing the concatenated port282does not close any of the argument ports.283284285---286Previous: [[Module (chicken plist)]]287288Next: [[Module (chicken pretty-print)]]